package com.haochu.novel.ui;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testnovel.R;
import com.haochu.novel.db.DBHelper;
import com.haochu.novel.util.Constants;
import com.haochu.novel.util.Constants.DIRECTORY_TYPE;
import com.haochu.novel.util.FileUtil;
import com.haochu.novel.vo.Book;
import com.haochu.novel.vo.Directory;

/**
 * @author fuhuichao
 * 2017年7月3日14:11:56
 * 浏览本地书籍目录
 */
public class BookDirectoryActivity extends Activity implements OnItemClickListener, OnClickListener {

	private Toast toast;
	private String rootPath;
	private MyAdapter myAdapter;
	private DBHelper dbHelper;
	// 文件目录数据
	private List<Directory> dirs;
	// 数据库已存在的书
	private List<Book> books;
	// 文件目录列表
	private ListView lv_directory;
	// 当前目录
	private TextView tv_filepath;
	// 加入书架按钮
	private Button btn_add;
	// 资源图片
	Drawable icon_check, icon_uncheck, icon_checkgray, icon_folder, icon_filetxt;
	
	@SuppressLint("ShowToast") 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_directory_list);

		// 查询数据库已存在的书
		dbHelper = DBHelper.getInstance(this);
		books = dbHelper.listBook();
		
//		rootPath = "/storage/emulated/0";
		rootPath = Environment.getExternalStorageDirectory().getPath();
		dirs = FileUtil.searchDirectory(rootPath, books);
		toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
		
		// 本地目录
		myAdapter = new MyAdapter(this);
		lv_directory = (ListView) findViewById(R.id.lv_directory);
		lv_directory.setAdapter(myAdapter);
		lv_directory.setOnItemClickListener(this);
		
		// 当前目录
		tv_filepath = (TextView) findViewById(R.id.tv_filepath);
		tv_filepath.setText(rootPath);
		
		// 加入书架
		btn_add = (Button) findViewById(R.id.btn_add);
		btn_add.setOnClickListener(this);
		
		// 初始化资源
		icon_check = getResources().getDrawable(R.drawable.check);
		icon_uncheck = getResources().getDrawable(R.drawable.un_check);
		icon_checkgray = getResources().getDrawable(R.drawable.check_gray);
		icon_folder = getResources().getDrawable(R.drawable.folder);
		icon_filetxt = getResources().getDrawable(R.drawable.file_txt);

		// 重新计算选择的文件数
		reCountChecked();
	}
	
	/**
	 * 自定义adapter
	 */
	class MyAdapter extends BaseAdapter{
		private LayoutInflater mInflater;
		private ImageView directoryType, checkType;
		private TextView title, desc;
		
		public MyAdapter(Context context){
			this.mInflater = LayoutInflater.from(context);
		}
		
		@Override
		public int getCount() {
			return dirs.size();
		}

		@Override
		public Object getItem(int position) {
			return dirs.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressLint("InflateParams") 
		@Override
		public View getView(int position, View convertView, ViewGroup viewGroup) {
			if(convertView == null){
				convertView = mInflater.inflate(R.layout.layout_directory_list_item, null);
			}
			
			// 初始化资源
			directoryType = (ImageView) convertView.findViewById(R.id.icon_directory_type);
			checkType = (ImageView) convertView.findViewById(R.id.icon_check_type);
			title = (TextView) convertView.findViewById(R.id.tv_title);
			desc = (TextView) convertView.findViewById(R.id.tv_desc);
			
			// 获取item对象
			Directory directory = dirs.get(position);
			
			// 绑定文字和图片
			title.setText(directory.getName());
			desc.setText(directory.getPath());
			if(directory.getType() == DIRECTORY_TYPE.file){
				directoryType.setImageResource(R.drawable.file_txt);
				if(directory.isExist()){
					checkType.setImageResource(R.drawable.check_gray);
				}else if(directory.isSelected()){
					checkType.setImageResource(R.drawable.check);
				}else{
					checkType.setImageResource(R.drawable.un_check);
				}
			}else if(directory.getType() == DIRECTORY_TYPE.dir){
				directoryType.setImageResource(R.drawable.folder);
				checkType.setImageResource(R.drawable.arrow_right);
			}
			
			return convertView;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Directory directory = dirs.get(position);
		
		if(directory.getType() == DIRECTORY_TYPE.dir){
			// 重新获取目录
			String clickPath = tv_filepath.getText() + "/" + directory.getName();
			tv_filepath.setText(clickPath);
			dirs = FileUtil.searchDirectory(clickPath, books);
		}else if(!directory.isExist()){
			// 切换标记选中状态
			directory.setSelected(!directory.isSelected());
		}

		// 重新计算选择的文件数
		reCountChecked();
		
		// 更新列表
		myAdapter.notifyDataSetChanged();
	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add:
			// 批量保存书
			boolean canDo = addBooks();
			if(canDo){
				// 返回书架页面
				setResult(Constants.RESULT_CODE_DIRECTORY);
				finish();
			}else{
				showTip("您还没有选中书籍哟~");
			}
			break;
		default:
			break;
		}
		
	}
	
	/**
	 * 重新计算选择的文件数
	 */
	public void reCountChecked() {
		int count = 0;
		for (Directory d : dirs) {
			if (d.isSelected()) {
				count++;
			}
		}
		btn_add.setText("导入书架(" + count + ")");
	}
	
	/**
	 * 批量保存
	 */
	public boolean addBooks(){
		List<Directory> list = new ArrayList<Directory>();
		for (Directory d : dirs) {
			if (d.isSelected()) {
				list.add(d);
			}
		}
		if(list.size() > 0){
			dbHelper.addBooks(list);
			return true;
		}
		return false;
	}
	
	/**
	 * 大土司
	 */
	public void showTip(String text){
		toast.setText(text);
		toast.show();
	}

}
